export function formatTime(timestamp: any) {
  const now: any = new Date(); // 获取当前时间
  const commentTime: any = new Date(timestamp); // 将评论时间戳转换为Date对象
  const timeDifference = now - commentTime; // 计算时间差（毫秒）

  if (timeDifference < 60000) { // 小于1分钟
    return '刚刚';
  } else if (timeDifference < 3600000) { // 小于1小时
    const minutes = Math.floor(timeDifference / 60000);
    return `${minutes}分钟前`;
  } else if (timeDifference < 86400000) { // 小于1天
    const hours = Math.floor(timeDifference / 3600000);
    return `${hours}小时前`;
  } else if (timeDifference < 259200000) { // 小于3天
    const date = Math.floor(timeDifference / 86400000);
    return `${date}天前`;
  } else {
    // 大于等于3天的情况，可以根据需要自定义格式
    const dateOptions = { year: '2-digit', month: '2-digit', day: '2-digit' };
    return commentTime.toLocaleDateString('zh-CN', dateOptions);
  }
}

export function numCount(num: any) {
  if (!num || num == 0) {
    return 0;
  } else if (0 < num && num < 100) {
    return num;
  } else if (99 < num && num < 100000) {
    const numK = parseFloat((num / 1000).toFixed(2))
    return numK + 'k';
  } else if (99999 < num && num < 1000000) {
    const numW = parseFloat((num / 10000).toFixed(2))
    return numW + 'w';
  } else if (999999 < num) {
    const numM = parseFloat((num / 1000000).toFixed(2))
    return numM + 'm';
  }
}
// 随机颜色
export function getRandomColor() {
  const letters = '0123456789ABCDEF';
  let color = '#';
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}

// 根据手机号后6位确定颜色
export const getNumColor = (id: any) => {
  // 取模操作，确保结果在0到0xFFFFFF之间
  let color = id % 0xFFFFFF;

  // 将数字转换为十六进制字符串，并确保长度为6位，不足则补零
  let hexColor = color.toString(16).padStart(6, '90');

  // 在字符串前加上#号
  return `#${hexColor}`;
}

// 随机数
export const getRandomNumber = (maxNum: number, minNum: number) => {
  return Math.floor(Math.random() * maxNum) + minNum;
}

/**
 * 时间戳
 */
export const timeDifference = (timestamp: number) => {
  const currentTime = new Date().getTime();
  const difference = timestamp - currentTime;

  if (difference < 0) {
    return "时间已过";
  }

  const seconds = Math.floor((difference / 1000) % 60);
  const minutes = Math.floor((difference / (1000 * 60)) % 60);
  const hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
  const days = Math.floor(difference / (1000 * 60 * 60 * 24));

  let result = '';
  if (days > 0) {
    result += `${days}天`;
  }
  if (hours > 0) {
    result += `${hours}小时`;
  }
  if (minutes > 0) {
    result += `${minutes}分钟`;
  }
  if (seconds > 0) {
    result += `${seconds}秒`;
  }

  return result || '刚刚';
}

// 获取地址后缀
export const getFileExtension = (url: any) => {
  if (!url) return false;
  const urlObj = new URL(url);
  const pathname = urlObj.pathname;
  const fileName = pathname.split('/').pop();
  const fileExtension = fileName?.split('.').pop();
  return fileExtension;
  // return fileName
}
// 
export const getQueryParams = (): Record<string, string> => {
  const queryString = window.location.search;
  const urlParams = new URLSearchParams(queryString);
  const paramsObj: Record<string, string> = {};

  for (const [key, value] of urlParams) {
    paramsObj[key] = value;
  }

  return paramsObj;
}

// 0到999的数字转换为中文表示
export const numberToChinese = (num: any) => {
  num = Number(num)
  const chineseNumbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  const units = ['', '十', '百', '千'];
  let str = '';
  if (num === 0) {
    return chineseNumbers[0];
  }

  for (let i = 0; num > 0; i++) {
    const digit = num % 10;
    if (digit !== 0) {
      str = chineseNumbers[digit] + units[i] + str;
    }
    num = Math.floor(num / 10);
  }
  // 去除末尾的零
  str = str.replace(/零+$/, '');
  return str.replace(/^一十/, '十');
}

// 手机号验证
export const isValidPhoneNumber = (phoneNumber: any) => {
  return /^1[3-9]\d{9}$/.test(phoneNumber);
}

// 保留手机号的前三位和后四位，其余部分用星号（*）替换
export const maskPhoneNumber = (phoneNumber: string): string => {
  // 检查输入是否为有效的手机号
  if (!/^\d{11}$/.test(phoneNumber)) {
    throw new Error("Invalid phone number format");
  }

  // 使用正则表达式进行替换
  return phoneNumber.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2');
}
// 实现将数字转换为对应的大写字母
export const numberToLetter = (num: number): string => {
  if (num < 1 || num > 26) {
    throw new Error("Input must be between 1 and 26");
  }
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return alphabet.charAt(num - 1);
}
// 获取终端机型
export const detectDeviceType = () => {
  const userAgent = navigator.userAgent;
  const platform = navigator.platform;
  const isMac = platform.toUpperCase().indexOf('MAC') >= 0;
  const isIOS = /iPad|iPhone|iPod/.test(userAgent) && !window.MSStream;
  const isAndroid = /Android/.test(userAgent);
  const isWindows = /Win32|Win64|Windows|WP/.test(platform);

  if (isMac) {
    return 'Mac';
  } else if (isIOS) {
    return 'iOS Device';
  } else if (isAndroid) {
    return 'Android Device';
  } else if (isWindows) {
    return 'Windows Device';
  } else {
    return 'Unknown Device';
  }
}